import java.util.HashMap;
import java.util.Map;
//525. 连续数组
public class Solution7 {
    public int findMaxLength(int[] nums) {
        int n = nums.length;
        Map<Integer,Integer> hash = new HashMap<>();   //存的前缀和，下标
        hash.put(0,-1);
        int sum = 0,ret = 0;
        for (int i = 0; i < n; i++) {
            sum += nums[i] == 0 ? -1 : 1;
            if (hash.containsKey(sum)) {
                ret = Math.max(ret,i-hash.get(sum));
            } else {
                hash.put(sum,i);
            }
        }
        return ret;
    }
}
